package Month11.Day25;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;
import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        String path = "D:\\中国玩家\\products.xml";
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        }
        List<Element> list = doc.selectNodes("/shop/product");
        Scanner in = new Scanner(System.in);
        System.out.println("请输入商品的编号：");
        int number = in.nextInt();
        boolean flag = true;
        for (Element e : list) {
            int itemo = Integer.parseInt(e.selectSingleNode("itemno").getText());
            if (itemo == number) {
                System.out.println("商品编号：" + e.selectSingleNode("itemno").getText() + "\t" +
                        "商品名字：" + e.selectSingleNode("name").getText() + "\t" +
                        "商品单价：" + e.selectSingleNode("price").getText() + "\t" +
                        "库存：" + e.selectSingleNode("stockcount").getText() + e.selectSingleNode("unit").getText());
                flag = false;
            }
        }
        if (flag) {
            System.out.println("输入编号错误，请重新输入");
        }
    }
}
